package cii_lib;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class CCII_INTERFACE_JSserver implements CCII_INTERFACE {

	private static final Logger log = LoggerFactory
			.getLogger(CCII_INTERFACE_JSserver.class);

	private JSserver _serv;

	public CCII_INTERFACE_JSserver(int step) {
		_serv = new JSserver(step);
	}

	public void HardwareOpen(String ip, int port, long base, int step) {
		try {
			log.debug("Opening hardware IP:{}, port:{}, base:{}, step:{}", new String[] {
					ip,
					""+port,
					""+base,
					""+step
			});
			_serv.Open(ip, port);
			
		} catch (JSserverException e) {
			log.error("", e);
			System.exit(0);
		}
	}

	public void HardwareClose() {
		try {
			log.debug("Closing hardware");
			_serv.Close();
		} catch (JSserverException e) {
			log.error("", e);
			System.exit(0);
		}
	}

	public long HardwareRead(long addr) {
		try {
			long result = _serv.Read(addr);
			log.info("Reading hardware address:{} result:{}", new String[]{Long.toString(addr), Long.toString(result)});
			return result;
		} catch (JSserverException e) {
			log.error("", e);
			HardwareClose();
			System.exit(0);
			return (0);
		}
	}

	public void HardwareWrite(long addr, long data) {

		try {
			log.info("Writing hardware address:{} data:{}", new String[]{Long.toString(addr), Long.toString(data)});
			_serv.Write(addr, data);
		} catch (JSserverException e) {
			log.error("", e);
			HardwareClose();
			System.exit(0);
		}
	}

}
